package com.service.user;

import com.dao.user.UserDao;
import com.dao.user.UserDaoImpl;
import com.enitiy.User;
import com.utils.page.Page;

import java.sql.SQLException;
import java.util.List;

/**
 * 创建作者 ：MySunshine
 * 创建时间 2022/10/24 11:20
 * 项目名称 SHUSHAN
 */
public class UserServiceImpl implements UserService {
    UserDao udi = new UserDaoImpl();
    @Override //用户登录
    public User UserLogin(int uNum, String pwd) {
        User user = null;
        try {
            user = udi.UserLogin(uNum, pwd);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }

    @Override //用户注册
    public boolean UserReg(User user) {
        boolean b = false;
        try {
            b = udi.UserReg(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return b;
    }

    @Override//用户注册 通过账号上传头像
    public boolean UserGetImgByNum(int num, String img) {
        boolean b = false;
        try {
            b = udi.UserGetImgByNum(num, img);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return b;
    }

    @Override//用户查询（全部）
    public List<User> UserSelectAll(Page p) {
        int userAllNum=0;
        try {
            userAllNum= udi.UserSelectAllNum(p);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        p.setTotalCounts(userAllNum);
        System.out.println("总条数"+ userAllNum);
        int totalPage=userAllNum%p.getPageSize()==0? userAllNum/p.getPageSize(): userAllNum/p.getPageSize()+1;
        p.setTotalPages(totalPage);
        System.out.println("总页数" + totalPage);
        List<User> users = null;
        try {
            users = udi.UserSelectAll(p);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return users;
    }

    @Override //删除用户
    public boolean UserDeleteByNum(int num) {
        boolean b = false;
        try {
            b = udi.UserDeleteByNum(num);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return b;
    }

    @Override // 根据账号修改用户
    public boolean UserUpdateByNum(User u) {
        boolean b = false;
        try {
            b = udi.UserUpdateByNum(u);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return b;
    }

    @Override // 根据账号查询用户
    public User UserSelectByNum(int Num) {
        User user = null;
        try {
            user = udi.UserSelectByNum(Num);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return user;
    }
}
